class $Q {
  constructor(dom) {
    this.dom = dom
  }

  addClass(className, classList) {
    const _classList = classList || this.getClassList()
    if (_classList.length && !_classList.includes(className)) {
      _classList.push(className)
      this.dom.className = _classList.join(' ')
    }
    return this
  }

  removeClass(className, classList) {
    const _classList = classList || this.getClassList()
    if (_classList.length) {
      const index = _classList.findIndex(item => item === className)
      if (index !== -1) {
        _classList.splice(index, 1)
        this.dom.className = _classList.join(' ')
      }
    }
    return this
  }

  toggleClass(className) {
    const classList = this.getClassList()
    if (classList.includes(className)) {
      this.removeClass(className, classList)
    } else {
      this.addClass(className, classList)
    }
    return this
  }

  getClassList() {
    const classNameStr = this.dom.className
    if (classNameStr) {
      return classNameStr.split(' ').filter(item => item)
    } else {
      return []
    }
  }
}

export default function (dom) {
  return new $Q(dom)
}
